package com.wxservice.serviceimpl;

import com.alibaba.fastjson.JSONObject;
import com.wxservice.domain.request.PublicAccounts;
import com.wxservice.service.BasicSupportService;
import com.wxservice.util.*;

import org.springframework.stereotype.Service;

@Service
public class BasicSupportServiceImpl implements BasicSupportService {

    @Override
    public String getBaseAccessToken(PublicAccounts publicAccounts) {
        String result = "failure";
        // 微信接口
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
        requestUrl = requestUrl.replace("APPID", publicAccounts.getAppID());
        requestUrl = requestUrl.replace("APPSECRET", publicAccounts.getAppSecret());
        // 返回值，调用微信接口，获取网页授权凭证
        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);

        if (null != jsonObject && (!jsonObject.containsKey("errcode"))) {
            result = jsonObject.toString();
        }else if(null != jsonObject && jsonObject.containsKey("errcode")){
            result =ErrorUtil.errorConversion(jsonObject);
        }
        return result;
    }


}
